package com.ht.web_vr_home_tour_backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.web_vr_home_tour_backend.obj.dto.PojInfoDto;
import com.ht.web_vr_home_tour_backend.obj.dto.ResData;
import com.ht.web_vr_home_tour_backend.obj.enums.PojType;
import com.ht.web_vr_home_tour_backend.obj.enums.UserLevel;
import com.ht.web_vr_home_tour_backend.obj.pojo.PojInfo;
import com.ht.web_vr_home_tour_backend.obj.vo.PojInfoVo;
import com.ht.web_vr_home_tour_backend.service.PojInfoService;
import com.ht.web_vr_home_tour_backend.service.PojRefObjService;
import com.ht.web_vr_home_tour_backend.utils.exceptions.MessageException;
import com.ht.web_vr_home_tour_backend.utils.interfaces.RequiredUserLevel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "项目控制器")
@RestController
@RequestMapping("/api/poj")
public class PojInfoController {
    private final PojInfoService pojInfoService;
    private final PojRefObjService pojRefObjService;

    public PojInfoController(PojInfoService pojInfoService, PojRefObjService pojRefObjService) {
        this.pojInfoService = pojInfoService;
        this.pojRefObjService = pojRefObjService;
    }

    @PostMapping
    @ApiOperation("保存/更新项目信息")
    @RequiredUserLevel(UserLevel.User)
    public ResData<Long> savePojInfo(@RequestBody PojInfoVo pojInfo) {
        long id = pojInfoService.savePojInfo(new PojInfo(pojInfo));
        List<String> refObjList = pojInfo.getRefObjList();
        if (refObjList != null && (!refObjList.isEmpty())) {
            refObjList.forEach(item -> {
                pojRefObjService.saveRefObj(id, Long.parseLong(item));
            });
        }
        return ResData.success(id);
    }

    @GetMapping("/{pojId}")
    @ApiOperation("通过id获取项目数据")
    @RequiredUserLevel(UserLevel.Tourists)
    public ResData<PojInfoDto> getPojInfoDto(@PathVariable("pojId") String pojId) {
        return ResData.success(pojInfoService.getInfoById(pojId));
    }

    @GetMapping("/page")
    @ApiOperation("分页获取项目数据")
    @RequiredUserLevel(UserLevel.Tourists)
    public ResData<Page<PojInfoDto>> pagePojInfoDto(
        @RequestParam(defaultValue = "1") String current,
        @RequestParam(defaultValue = "10") String size,
        @RequestParam(required = false) PojType type
    ) {
        Page<PojInfoDto> dtoPage = pojInfoService.pagePojInfoDto(Long.parseLong(current), Long.parseLong(size), type);
        return ResData.success(dtoPage);
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除项目数据")
    @RequiredUserLevel(UserLevel.User)
    public ResData<Void> deletePojInfo(@PathVariable("id") String id) {
        pojInfoService.deletePojInfoById(id);
        return ResData.success();
    }

    @GetMapping("/type")
    @ApiOperation("获取项目类型")
    public ResData<List<String>> getPojType() {
        List<String> list = Arrays.stream(PojType.values())
                .map(Enum::name)
                .collect(Collectors.toList());
        return ResData.success(list);
    }

    @PostMapping("/obj/{pojId}")
    @ApiOperation("向项目中添加对象的引用")
    @RequiredUserLevel(UserLevel.User)
    public ResData<Void> addObjToPoj(@PathVariable("pojId") Long pojId,
                                     @RequestBody List<String> objIdList) {
        objIdList.forEach(item -> {
            pojRefObjService.saveRefObj(pojId, Long.parseLong(item));
        });
        return ResData.success();
    }

    @PostMapping("/obj/remove/{pojId}")
    @ApiOperation("从项目中移除对象引用")
    @RequiredUserLevel(UserLevel.User)
    public ResData<Void> removeObjInPoj(
            @PathVariable Long pojId,
            @RequestBody List<String> objIdList
    ) {
        objIdList.forEach(item -> {
            if (!pojRefObjService.removeRefObj(pojId, Long.parseLong(item))) {
                throw new MessageException("删除失败，请重试");
            }
        });
        return ResData.success();
    }
}
